<script setup>

import {getCurrentInstance, ref} from "vue";
import {clearSysLoginLog, deleteSysLoginLog, listSysLoginLog} from "@/api/system/log/login_log";
import {loginLogStatusList} from "@/enum/enum";
import OverflowTooltip from "@/components/OverflowTooltip/index.vue";
import {enumToArray} from "@/utils/utils";

const {proxy} = getCurrentInstance();
const sysLoginLogList = ref([])
const total = ref(0)
const queryFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  loginLocation: null,
  userName: null,
  status: null,
  timeArr: null,
})
const ids = ref([])
const loginStatusOptions = enumToArray(loginLogStatusList);

const getList = async () => {
  const {data} = await listSysLoginLog(queryForm.value);
  total.value = data.total;
  sysLoginLogList.value = data.list;
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.logId);
}

const handleDelete = () => {
  const logId = ids.value;
  proxy.$modal.confirm(`确定删除选中的登录日志吗？`).then(async () => {
    await deleteSysLoginLog(logId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleClear = () =>{
  proxy.$modal.confirm(`确定清空登录日志吗？`).then(async () => {
    await clearSysLoginLog();
    proxy.$modal.okNotify('清空成功');
    await getList();
  })
}

getList()

</script>

<template>
  <div class="app-container">

    <el-form :model="queryForm" ref="queryFormRef" :inline="true" class="demo-form-inline">
      <el-form-item label="登录地址" prop="loginLocation">
        <el-input v-model="queryForm.loginLocation" placeholder="请输入登录地址"/>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="queryForm.userName" placeholder="请输入用户名称"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryForm.status" placeholder="请选择状态">
          <el-option v-for="(item, index) in loginStatusOptions" :key="index" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="登录时间" prop="timeArr">
        <el-date-picker
            v-model="queryForm.timeArr"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
          查询
        </el-button>
        <el-button plain @click="handleReset">
          <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div class="flex mb-5">
      <el-button size="default" type="danger" plain :disabled="!ids.length" class="flex items-center" @click="handleDelete" v-hasPermission="['system:login_log:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </el-button>
      <el-button size="default" type="danger" plain class="flex items-center" @click="handleClear" v-hasPermission="['system:login_log:clear']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        清空
      </el-button>
    </div>

    <el-table :data="sysLoginLogList" :border="true" size="large" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="logId" label="访问编号"/>
      <el-table-column prop="userName" label="用户名称"/>
      <el-table-column prop="ipAddr" label="登录地址"/>
      <el-table-column prop="loginLocation" label="登录地点"/>
      <el-table-column prop="browser" label="浏览器"/>
      <el-table-column prop="os" label="操作系统"/>
      <el-table-column prop="status" label="登录状态">
        <template #default="scope">
          <el-tag :type="loginLogStatusList[scope.row.status].type">{{ loginLogStatusList[scope.row.status].label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="msg" label="操作信息">
        <template #default="scope">
          <OverflowTooltip :content="scope.row.msg"/>
        </template>
      </el-table-column>
      <el-table-column prop="loginTime" label="登录日期" width="220"/>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryForm.pageNo"
        v-model:limit="queryForm.pageSize"
        @pagination="getList"/>

  </div>
</template>

<style scoped lang="scss">

</style>
